package icrts.universe;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Timer;
import java.util.TimerTask;
import icrts.logic.*;
import icrts.GameManager;
import icrts.logic.AbstractGameObject;

/**
 * Universe Class. A singleton class.
 * @author Rudolf Lai
 */
public class Universe {

    Timer time;

    private static final int UPDATE_FREQ = 30;
    private int i = 10;
    private int j = 10;

    private HashMap<Integer, HashSet<AbstractGameObject>> timeline
            = new HashMap<Integer, HashSet<AbstractGameObject>>();
    /**
     * Private constructor prevents instantiation from other classes.
     */
    private Universe() {}
    /**
    * UniverseSingleton is loaded on the first execution of Universe.getInstance()
    * or the first access to UniverseSingleton.INSTANCE, not before.
    */
    private static class UniverseSingleton {
        private static final Universe INSTANCE = new Universe();
    }
   /**
    *
    * @return the Universe.
    */
    public static Universe getInstance() {
        return UniverseSingleton.INSTANCE;
    }
    public void bigBang() {
        time = new Timer();
        time.schedule(new MoveTask(), 30);
    }

    class MoveTask extends TimerTask {
        public void run() {
            HashSet<AbstractGameObject> newGameState
                = new HashSet<AbstractGameObject>();
            newGameState.add(new SampleBuilding(new Position(i, j)));
            i += 1;
            j += 1;
            GameManager.setGameState(newGameState);
            GameManager.gameMap.update();
            time.schedule(new MoveTask(), 30);
        }
    }
   /**
    *
    * @param state which state in time does the object goes into.
    * @param obj the object that would be located in that state and updated.
    */
    public void addToState(int state, AbstractGameObject obj) {
        HashSet<AbstractGameObject> currentState =
            timeline.get(state);
        currentState.add(obj);
    }

    public HashSet<AbstractGameObject> getState(int state) {
        return timeline.get(state);
    }
}
